#include "detectiondao.h"

DetectionDao::DetectionDao() {}

/*
 *     int         id;
    int         seat_id;
    QString     position;
    QString     detectTime;
    QString     note;
*/

int DetectionDao::insert(Detection &detection)
{
    // 插入数据
    QString insertQuery = "INSERT INTO detection(seatid, position, detecttime, note) VALUES "
                          "(:seatid, :position, :detecttime, :note)";
    //qDebug() << insertQuery;
    query.prepare(insertQuery);
    query.bindValue(":seatid", detection.getSeatId());
    query.bindValue(":position", detection.getPosition());
    query.bindValue(":detecttime", detection.getDetectTime());
    query.bindValue(":note", detection.getNote());
    qDebug() << QString("%1,%2,%3,%4")
                    .arg(detection.getSeatId())
                    .arg(detection.getPosition())
                    .arg(detection.getDetectTime())
                    .arg(detection.getNote());

    if (!query.exec()) {
        qDebug() << QString("插入数据到 detection 表中失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return -1;
    }
    qDebug() << QString("插入数据到 detection 表中成功，最后一条记录的id是：%1 。")
                    .arg(query.lastInsertId().toInt());
    return query.lastInsertId().toInt();
}

int DetectionDao::updateByPrimay(Detection &detection)
{
    QString updateQuery
        = "UPDATE detection Set seatid = ?, position = ?, detecttime = ?, note = ? where id = ?";
    //qDebug() << updateQuery;
    query.prepare(updateQuery);
    query.addBindValue(detection.getSeatId());
    query.addBindValue(detection.getPosition());
    query.addBindValue(detection.getDetectTime());
    query.addBindValue(detection.getNote());
    query.addBindValue(detection.getId());
    qDebug() << QString("%1,%2,%3,%4,%5")
                    .arg(detection.getSeatId())
                    .arg(detection.getPosition())
                    .arg(detection.getDetectTime())
                    .arg(detection.getNote())
                    .arg(detection.getId());
    if (!query.exec()) {
        qDebug() << QString("更新数据到 detection 表中失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return -1;
    }
    return 1;
}

int DetectionDao::deleteByPrimay(Detection &detection)
{
    QString deleteQuery = "DELETE FROM detection where id = ?";
    //qDebug() << deleteQuery;
    query.prepare(deleteQuery);
    query.addBindValue(detection.getId());
    qDebug() << QString("%1").arg(detection.getId());
    if (!query.exec()) {
        qDebug() << QString("删除表格 detection 数据失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return -1;
    }
    return 1;
}

int DetectionDao::queryAll(QList<Detection *> &list)
{
    int count = 0;
    // 查询数据
    QString selectQuery = "SELECT * FROM detection";
    //qDebug() << selectQuery;
    if (!query.exec(selectQuery)) {
        qDebug() << QString("查询表格 detection 数据失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return count;
    }

    while (query.next()) {
        count++;
        int id = query.value("id").toInt();
        int seatid = query.value("seatid").toInt();
        QString position = query.value("position").toString();
        QString detecttime = query.value("detecttime").toString();
        QString note = query.value("note").toString();
        Detection *pDetection = new Detection(id, seatid, position, detecttime, note);
        list.append(pDetection);
    }

    return count;
}

int DetectionDao::queryBySeatId(QList<Detection *> &list, int seatId)
{
    int count = 0;
    // 查询数据
    QString selectQuery = QString("SELECT * FROM detection where seatid = '%1'").arg(seatId);
    //qDebug() << selectQuery;
    if (!query.exec(selectQuery)) {
        qDebug() << QString("查询表格 detection 数据失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return count;
    }

    while (query.next()) {
        count++;
        int id = query.value("id").toInt();
        int seatid = query.value("seatid").toInt();
        QString position = query.value("position").toString();
        QString detecttime = query.value("detecttime").toString();
        QString note = query.value("note").toString();
        Detection *pDetection = new Detection(id, seatid, position, detecttime, note);
        list.append(pDetection);
    }

    return count;
}
